package com.FH.applyForm.officeServies.carManage_secondType;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/19.
 */
@Stateless
public class CarManagesDAOImpl extends AccessDB implements CarManagesDAO{
    /**
     * 查询二级车辆管理信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getCarManagesInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from carsecond_type  where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok+1)+","+size+"";
            }

            if(json.has("carfirst_id")){
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND carfirst_id ='" + json.getString("carfirst_id") + "'";
            }

            if(json.has("carsecond_id")){
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND carsecond_id ='" + json.getString("carsecond_id") + "'";
            }

            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();

            List<CarManagesShowDTO> result = new ArrayList<CarManagesShowDTO>();

            if(!existName.equals("0")){
                String sql ="SELECT carsecond_id,carsecondtype_name,carfirst_id,carsite FROM carsecond_type WHERE 1=1 "+sql_w +sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    CarManagesShowDTO carManagesShowDTO = new CarManagesShowDTO();
                    String carsecond_id = seresult.get(i)[1];
                    carManagesShowDTO.setCarsecond_id(carsecond_id);
                    carManagesShowDTO.setCarsecondtype_name(seresult.get(i)[2]);
                    carManagesShowDTO.setCarfirst_id(seresult.get(i)[3]);
                    carManagesShowDTO.setCarsite(seresult.get(i)[4]);
                    result.add(carManagesShowDTO);

                }
            }
            if(seresult.size()!=0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }


    /**
     * 添加二级车辆管理信息
     * @param carManagesPullDTO
     * @return
     */
    @Override
    public ResultDTO addCarManagesInfo(CarManagesPullDTO carManagesPullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String carsecondtype_name=carManagesPullDTO.getCarsecondtype_name();
        String sql="select * from carsecond_type where carsecondtype_name='"+carsecondtype_name+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("此车辆信息已存在，请重新分配");
            return resultDTO;
        }
        String sql1="";
        sql1="insert into carsecond_type (carsecond_id,carsecondtype_name,carfirst_id,carsite)" +
                "values('"+uuid+"','"+carManagesPullDTO.getCarsecondtype_name()+"','"+carManagesPullDTO.getCarfirst_id()+"','"+carManagesPullDTO.getCarsite()+"')";

        resultls=addReportingTo(sql1);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改二级车辆管理信息
     * @param carManagesPullDTO
     * @return
     */
    @Override
    public ResultDTO updateCarManagesInfo(CarManagesPullDTO carManagesPullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update carsecond_type set carsecond_id = '" + carManagesPullDTO.getCarsecond_id() + "'";
        if(null!=carManagesPullDTO.getCarsecondtype_name()&&!"".equals(carManagesPullDTO.getCarsecondtype_name())){
            QUERY += ", carsecondtype_name = '" + carManagesPullDTO.getCarsecondtype_name() + "'";
        }
        if(null!=carManagesPullDTO.getCarsite()&&!"".equals(carManagesPullDTO.getCarsite())){
            QUERY += ", carsite = '" + carManagesPullDTO.getCarsite() + "'";
        }
        QUERY += " where carsecond_id = '" + carManagesPullDTO.getCarsecond_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除二级车辆管理信息
     * @param carManagesPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteCarManagesInfo(CarManagesPullDTO carManagesPullDTO) {
        //删除之前首先查询该表中是否有记录

        String sql="select count(1) from carsecond_type WHERE carsecond_id='"+carManagesPullDTO.getCarsecond_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from carsecond_type  where carsecond_id='"+carManagesPullDTO.getCarsecond_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.delete_success);
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
